package com.pengheng.transformation.partition;

import org.apache.flink.api.common.functions.Partitioner;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class PartitionCustomDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        DataStreamSource<String> socketDS = env.socketTextStream("192.168.10.100", 7777);

        socketDS.partitionCustom(new MyPartitioner(), new MyKeySelector());

        socketDS.print();

        env.execute();
    }

    static class MyPartitioner implements Partitioner<String> {


        @Override
        public int partition(String key, int i) {
            return Integer.parseInt(key) % i;
        }
    }

    private static class MyKeySelector implements KeySelector<String, String> {

        @Override
        public String getKey(String s) {
            return s;
        }
    }
}
